$( function ( ) {
  $( '#myTab a' )
    .click( function ( e ) {
      e.preventDefault( );
      $( this )
        .tab( 'show' );
    } ); //Tab

  $( window )
    .scroll( function ( ) {
      // alert(window.scrollY);
      if ( window.scrollY > 100 ) {
        $( 'footer button#back-top' )
          .show( 'fast' );
      } else {
        $( 'footer button#back-top' )
          .hide( 'fast' );
      }
    } ); //Back-top

  /*$( 'span.heart' )
    .click( function ( ) {
      var $count = $( this )
        .next( )
        .next( );
      var num = parseInt( $count.text( ) ) + 1;
      $count.text( num );
    } ); *///count-zan
  (function() {
    function heartActive() {
      $('.heart').each(function(index, el) {
        var $el = $(el);
        $(el).data('iszan') == 1 ? $el.addClass('heart-active') : $el.removeClass('heart-active');
      });
    }
    heartActive();
  })();

  $( '#write-mes' )
    .on( 'click', function ( ) {
      $( '#recipient-name' )
        .val( "" );
      $( '#recipient-name' )
        .attr( 'disabled', false );
    } ); //disable-form

  //correct-position

  $( 'div.action' )
    .on( 'click', 'span.total', function ( ) {
      $( 'div.list div.mess-list' )
        .each( function ( index, el ) {
          // alert($(this).html());
          if ( $( this )
            .find( 'div.mes-two-sides' )
            .find( 'span.name' )
            .text( ) == '我' ) {
            $( this )
              .find( 'div.action' )
              .empty( )
              .append( '<span class="del" data-toggle="modal" data-target="#Modal-del-detail">删除</span>' );
          }
        } ); //correct-position
      var name = $( this )
        .parent( )
        .prev( )
        .find( '.name' )
        .text( );
      // $('div.col-md-7').toggleClass('mes-info');
      $( 'div.my-mes>span.mes-to>span.to-name' )
        .text( name );
      $( '#post-cont' )
        .focus( );
    } ); //focus-form

  $( 'div.pro div.action' )
    .on( 'click', 'span.res', function ( ) {
      var userName = $( this )
        .parent( )
        .prev( )
        .find( '.name' )
        .text( );
      $( '#recipient-name' )
        .val( userName );
      $( '#recipient-name' )
        .attr( 'disabled', true );
    } ); //res-form-enable


  $( 'div.list div.action' )
    .on( 'click', 'span.res', function ( ) {
      $( '#post-cont' )
        .focus( );
    } ); //focus-form-res



  $( '.form-group' )
    .on( 'click', 'button#send', function ( ) {
      // alert('good');
      $( '#post-cont' )
        .siblings( )
        .remove( );
      if ( $( '#post-cont' )
        .val( )
        .match( /^\s*$/ ) ) {
        var tip = '<span class="tip pull-right">请填写私信内容</span><div class="clear-fix"></div>';
        $( '#post-cont' )
          .after( tip );
      }
    } ); //send-tips

  $( '#post-cont' )
    .on( 'focus', function ( ) {
      $( 'span.tip,span.tip+div.clear-fix' )
        .remove( );
    } ); //remove-tips

  $( 'div.action' )
    .on( 'click', 'span.del', function ( ) {
      var name = $( this )
        .parent( )
        .prev( )
        .find( '.name' )
        .text( );
      $( '.modal-body span.name' )
        .text( name );
    } );
  $( 'div.my-mes>div.back' )
    .on( 'click', function ( ) {
      $( 'div.col-md-7' )
        .toggleClass( 'mes-info' );
    } ); //del-modal

  // $("#draggable,#draggable-del,#draggable-del-detail").draggable();

  function jubao( ) {
    var html = '<ul class="subnav"><li><a href="#">广告等垃圾信息</a></li><li><a href = "#">不友善内容</a></li><li><a href = "#">其他</a></li></ul>';
    $( 'body' )
      .append( html );
    $( '.subnav' )
      .hide( );
    $( 'span.jubao' )
      .on( 'mouseover', function ( ) {
        // var x = $(this).position().left,
        // y = $(this).position().top + $(this).outerHeight();
        var x = $( this )
          .offset( )
          .left,
          y = $( this )
          .offset( )
          .top + $( this )
          .outerHeight( );
        // alert('x=' + x + '\ny=' + y);
        $( '.subnav' )
          .css( {
            'position': 'absolute',
            'left': x,
            'top': y
          } );
        $( '.subnav' )
          .show( );
      } );
    $( 'span.jubao' )
      .mouseout( function ( ) {
        $( '.subnav' )
          .hide( );
      } );
    $( '.subnav' )
      .mouseover( function ( ) {
        $( this )
          .show( );
      } );
    $( '.subnav' )
      .mouseout( function ( ) {
        $( this )
          .hide( );
      } );
  }
  jubao( );

  //点赞ajax
  $('.choice-content').on('click', '.heart', function(event) {
    var $this = $(this),
        $parent = $this.parent(),
        groupId = $parent.data('groupid');

    $.ajax({
      url: '/topic/tozan?id=' + groupId,
      type: 'GET',
      dataType: 'json'
    })
    .done(function(data) {
      var zanCount = data.zan,
          statu = data.success;
      $parent.find('.heart-count').text(zanCount);
      statu == 1 ? $this.addClass('heart-active') : (statu === 2 ? $this.removeClass('heart-active') : console.log('zan wrong'));
    })
    .fail(function() {
      console.log("error");
    });
    
  });

  //加入小组ajax：th页面上方的加入按钮
  $('#join-or-leave').on('click', function(event) {
    var userId = this.dataset.userid,
        groupId = this.dataset.groupid,
        $this = $(this);

    $.ajax({
      url: '/group/intoGroup?group_id=' + groupId,
      type: 'POST',
      dataType: 'json'
    })
    .done(function(data) {
      if (data.success == 1) {
        $this.find('.text').text('我是小组成员');
        $this.prop('disabled', 'disabled');
      }
    })
    .fail(function() {
      console.log("error");
    });
  });

  //侧栏推荐小组：ct，th页面
  $('.th-guess').on('click', '.th-join', function(event) {
    var userId = $('#join-or-leave .star').data('userid'),
        groupId = this.dataset.groupid,
        $this = $(this);

    $.ajax({
      url: '/group/intoGroup?group_id=' + groupId,
      //url: '/group/group/join.json',
      type: 'POST',
      dataType: 'json'
    })
    .done(function(data) {
      if (data.success == 1) {
        $this.html('<span class="glyphicon glyphicon-ok"></span> 我是小组成员');
        $this.prop('disabled', 'disabled');
      }
    })
    .fail(function() {
      console.log("error");
    });
  });

  //team_home页面分页ajax
  $('.th-page').on('click', 'a', function(event) {
    event.preventDefault();
    var $this = $(this),
        $parent = $this.parent(),
        page = $this.text(),
        groupId = $('#joinOrLeave .star').data('groupid'),
        curPage;

    $('.th-page a').each(function(index, el) {
      var $el = $(el);
      if ($el.hasClass('active')) {
        curPage = $el.text();
      }
    });
    if ($parent.is('.th-page :first-child')) {
      page = parseInt(curPage) - 1;
    } else if ($parent.is('.th-page :last-child')) {
      page = parseInt(curPage) + 1;
    }


    $.ajax({
      url: '/group/getTopicByweb?group_id=' + groupId +'&page=' + page,
      // url: '/group/group/page.json',
      type: 'GET',
      dataType: 'json'
    })
    .done(function(data) {
      //underscore模板加载
      var templateTime, templateHot;

      if (data.success == 1) {
        templateTime = _.template($('#topic-time').html());
        templateHot = _.template($('#topic-hot').html());

        $('#latest-topic').html(templateTime({"getTime": data.getTime}));
        $('#hottest-topic').html(templateHot({"getHot": data.getHot}));
      }

      $this.parent().addClass('active')
        .siblings().removeClass('active');
    })
    .fail(function() {
      console.log("error");
    });
    
  });

  //post-content页面评论分页ajax
  (function() {
    var page = 1;
    $('.pc-page').on('click', 'button', function(event) {
      event.preventDefault();
      
      var $this = $(this),
          pageRequest = $this.hasClass('pc-prev') ? --page :　++page,
          topicId = $('.pc-topic').data('topicid');

      $.ajax({
        url: '/topic/getCommentsPage?page=' + pageRequest + '&topic_id=' + topicId,
        // url: '/group/group/page-comment.json',
        type: 'GET',
        dataType: 'json'
      })
      .done(function(data) {
        var $prev = $('.pc-prev'),
            $next = $('.pc-next'),
            template;
        if (page === 1) {
          $prev.prop('disabled', 'disabled');
        } else {
          $prev.prop('disabled', '');
        }

        if (data.success == 2) {
          $next.prop('disabled', 'disabled');
        } else {
          $next.prop('disabled', '');
        }

        template = _.template($('#temp-comment').html());
        $('.pc-comments').html(template({"comments": data.comments, "iscreater": data.iscreater}));
      })
      .fail(function() {
        console.log("comment error");
      });

    });
  })();

} );

